<?php

class CExpressOrder extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	public function __construct(){
		
		$this->_name=R_PRE."express_orders";
		
		$this->_primary="orderid";
		
		parent::__construct();
		
	}

	public function createOrder($data){
		
		$orderid=$this->insert($data);
		
		return $orderid;
		
	}
	
	public function editOrder($orderid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('orderid = ?', $orderid);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getDetail($orderid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name);
		
		$select->joinLeft('express_companies',"$this->_name.carrier=express_companies.companyid",'*');
		
		$select->joinLeft(R_PRE.'express_orderfls',"$this->_name.stateid=".R_PRE.'express_orderfls'.".flid",'*');
		
		$select->joinLeft(R_PRE.'locationfls AS A',"$this->_name.to_station_id=A.flid",'A.en_flname AS to_station_name');
		
		$select->where('orderid = ? ',$orderid);
		
		return $db->fetchAll($select);
		
	}
	
	public function delOrders($orderidArray){
		
		$db=$this->getAdapter();
		
		$orderidString=join(',',$orderidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE orderid IN ($orderidString)");
		
		return $result;
		
	}
	
	public function getAllOrderCounts($where=null){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'COUNT(*)');
		
		if($where!=null){
		
			$select->where($where);
			
		}
		
		return $db->fetchOne($select);
		
	}
	
	public function getAllOrders($from,$counts,$where=null){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		if($where!=null){
		
			$select->where($where);
			
		}
		
		$select->joinLeft(R_PRE.'express_orderfls',"$this->_name.stateid=".R_PRE.'express_orderfls'.".flid",'*');
		
		$select->joinLeft(R_PRE.'locationfls AS A',"$this->_name.to_station_id=A.flid",'A.en_flname AS to_station_name');
		
		$select->order("orderid DESC");
		
		if($counts!=0){
			
			$select->limit($counts, $from);
			
		}
		
		return $db->fetchAll($select);
		
	}
	

	
}